from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from model.ModWorkRecord import WorkRecord, workRecordSchema
from model.ModRepeatProDetail import RepeatProDetail, repeatProDetailSchema
from model.ModWorkrecordConfirm import workrecordConfirmSchema, WorkrecordConfirm
from database import Session, engine
import datetime
from sqlalchemy.sql import text
from sqlalchemy import func, null
import pendulum


def get_dep_workload_detail(session, begin_time: datetime, end_time: datetime, bureauid: str, flag: str):
    if flag == "1":
        # 问题总数量
        dep_workload_detail = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.opentime >= begin_time).filter(WorkRecord.opentime <= end_time).order_by(
            WorkRecord.opentime.asc()).all()
    elif flag == "2":
        # 待处理数量
        dep_workload_detail = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "0").filter(WorkRecord.opentime >= begin_time).filter(
            WorkRecord.opentime <= end_time).order_by(
            WorkRecord.opentime.asc()).all()
    else:
        # 已处理数量
        dep_workload_detail = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "1").filter(WorkRecord.opentime >= begin_time).filter(
            WorkRecord.opentime <= end_time).order_by(WorkRecord.opentime.asc()).all()
    return JSONResponse(workRecordSchema.dump(dep_workload_detail, many=True).data)

######按供电所分类的项目部工作量统计明细
def get_dep_workload_station_detail(session, begin_time: datetime, end_time: datetime, bureauid: str, stationid: str,
                                    flag: str):
    if flag == "1":
        # 问题总数量
        dep_workload_detail = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.stationid == stationid).filter(
            WorkRecord.opentime >= begin_time).filter(WorkRecord.opentime <= end_time).order_by(
            WorkRecord.opentime.asc()).all()
    elif flag == "2":
        # 待处理数量
        dep_workload_detail = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.stationid == stationid).filter(
            WorkRecord.final_result_flag == "0").filter(WorkRecord.opentime >= begin_time).filter(
            WorkRecord.opentime <= end_time).order_by(
            WorkRecord.opentime.asc()).all()
    else:
        # 已处理数量
        dep_workload_detail = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.stationid == stationid).filter(
            WorkRecord.final_result_flag == "1").filter(WorkRecord.opentime >= begin_time).filter(
            WorkRecord.opentime <= end_time).order_by(WorkRecord.opentime.asc()).all()
    return JSONResponse(workRecordSchema.dump(dep_workload_detail, many=True).data)


def get_emp_workload_detail(session, begin_time: datetime, end_time: datetime, bureauid: str, flag: str, workid: str):
    if flag == "1":
        # 问题总数量
        emp_workload_detail = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.opentime >= begin_time).filter(WorkRecord.opentime <= end_time).filter(
            WorkRecord.openid == workid).order_by(
            WorkRecord.opentime.asc()).all()
    elif flag == "2":
        # 待处理数量
        emp_workload_detail = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "0").filter(WorkRecord.opentime >= begin_time).filter(
            WorkRecord.opentime <= end_time).filter(WorkRecord.openid == workid).order_by(
            WorkRecord.opentime.asc()).all()
    else:
        # 已处理数量
        emp_workload_detail = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "1").filter(WorkRecord.opentime >= begin_time).filter(
            WorkRecord.opentime <= end_time).filter(WorkRecord.openid == workid).order_by(
            WorkRecord.opentime.asc()).all()
    return JSONResponse(workRecordSchema.dump(emp_workload_detail, many=True).data)


def get_repeat_pro_detail(session, begin_time: datetime, end_time: datetime, bureauid: str):
    # 重复问题处理明细
    pro_list = session.query(RepeatProDetail).filter(RepeatProDetail.bureauid == bureauid).filter(
        RepeatProDetail.opentime >= begin_time).filter(RepeatProDetail.opentime <= end_time).order_by(
        RepeatProDetail.meter_barcode.asc()).all()
    return JSONResponse(repeatProDetailSchema.dump(pro_list, many=True).data)


def insert_work_record(session, work):
    # 新增工作记录
    try:
        session.add(work)
    except Exception as e:
        raise


def get_workrecord_list(session, bureauid: str, flag: str):
    # 项目经理审核页面员工工作记录列表   # 1待确认记录  2已确认未抄回记录
    workrecord_list = ""
    if flag == "1":
        workrecord_list = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "0").filter(
            WorkRecord.confirm_openid.is_(None)).order_by(WorkRecord.opentime.desc()).all()
    else:
        workrecord_list = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "0").filter(
            WorkRecord.confirm_openid.isnot(None)).order_by(WorkRecord.opentime.desc()).all()
    return JSONResponse(workRecordSchema.dump(workrecord_list, many=True).data)


def update_workrecord_info(session, serial_number: str, initial_result_flag: str, final_result_flag: str,
                           work_department: str, confirm_openid: str, confirm_openname: str,remark: str):
    # 项目经理审核页面更新是否抄回等标志
    try:
        wrecord = session.query(WorkRecord).filter(WorkRecord.serial_number == serial_number).one_or_none()
        wrecord.initial_result_flag = initial_result_flag
        wrecord.final_result_flag = final_result_flag
        wrecord.work_department = work_department
        wrecord.confirmtime = pendulum.now()
        wrecord.confirm_openid = confirm_openid
        wrecord.confirm_openname = confirm_openname
        wrecord.remark = remark
    except Exception as e:
        raise


def get_pending_pro_detail(session, bureauid: str, stationid: str, flag: str):
    # 待处理问题明细
    pending_pro = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
        WorkRecord.stationid == stationid).filter(WorkRecord.final_result_flag == "0").filter(
        WorkRecord.work_department == flag).filter(WorkRecord.confirm_openid.isnot(None)).all()
    return JSONResponse(workRecordSchema.dump(pending_pro, many=True).data)


def get_pending_pro_suptype_detail(session, bureauid: str, stationid: str, problemid: str):
    # 待处理问题明细故障类
    if stationid == "00":
        pending_pro = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "0").filter(
            WorkRecord.problemid == problemid).filter(WorkRecord.confirm_openid.isnot(None)).all()
    else:
        pending_pro = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.stationid == stationid).filter(WorkRecord.final_result_flag == "0").filter(
            WorkRecord.problemid == problemid).filter(WorkRecord.confirm_openid.isnot(None)).all()
    return JSONResponse(workRecordSchema.dump(pending_pro, many=True).data)


def get_meter_info(session, bureauid: str, meter_barcode: str):
    # 根据条码查询详细信息
    meter_info = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
        WorkRecord.meter_barcode.like('%' + meter_barcode + '%')).order_by(
        WorkRecord.opentime.asc()).all()
    return JSONResponse(workRecordSchema.dump(meter_info, many=True).data)


def get_pending_count(session, bureauid: str):
    # 查询县局未处理问题总数(只显示未确认过的数量)
    if bureauid == "00":
        pending_count = session.query(WorkRecord).filter(WorkRecord.final_result_flag == "0").filter(
            WorkRecord.confirm_openid.is_(None)).all()
        list_pending = workRecordSchema.dump(pending_count, many=True).data
        return len(list_pending)
    else:
        pending_count = session.query(WorkRecord).filter(WorkRecord.bureauid == bureauid).filter(
            WorkRecord.final_result_flag == "0").filter(WorkRecord.confirm_openid.is_(None)).all()
        list_pending = workRecordSchema.dump(pending_count, many=True).data
        return len(list_pending)


def get_workrecord_confirm(session):
    # # 项目部确认工作量
    confirm_list = session.query(WorkrecordConfirm).order_by(
        WorkrecordConfirm.bureauid.asc()).all()
    return JSONResponse(workrecordConfirmSchema.dump(confirm_list, many=True).data)


def get_pro_other_detail(session, bureauid: str, flag: str):
    # 其它类问题明细
    if flag == "1":
        # 其它问题
        pro_other_detail = session.query(WorkRecord).filter(
            WorkRecord.bureauid == bureauid).filter(WorkRecord.problemid == "99").order_by(
            WorkRecord.opentime.asc()).all()
    else:
        # 其它解决方法
        pro_other_detail = session.query(WorkRecord).filter(
            WorkRecord.bureauid == bureauid).filter(WorkRecord.resolventid == "99").order_by(
            WorkRecord.opentime.asc()).all()
    return JSONResponse(workRecordSchema.dump(pro_other_detail, many=True).data)


